<template>
	<div class="container">
		<div class="header">
			 <span @click="$router.replace('/Profile')">
                        <img src="../../assets/image/icon_arrow_left_black.png" alt="" />
                         </span>
			<span>收货地址</span>
		</div>
			<div class="content">
				<ul class="list">
					<li v-for="item in $store.state.address.list" :key="item.id" :class="{default:item.isDefault===1}">
						<h6 v-text="item.receiveName"></h6>
						<p class="phone" v-text="item.receivePhone"></p>
						<p>{{item.receiveRegion}}-{{item.receiveDetail}}</p>

						<button @click="remove(item.id)"><i class="iconfont icon-remove"></i></button>
						<p  class="editbtn" @click="beginUpdate(item)"><i class="iconfont icon-edit"></i></p>
						<p class="defaults" v-if="item.isDefault===1">默认</p>
						<button v-else @click="setDefault(item.id)">设为默认地址</button>
					</li>
				</ul>
			</div>

		<AddressEdit
		ref="edit"
		 v-show="isEdit"
		@close="isEdit=false"
		 @save="save">
		</AddressEdit>
		<div class="footer">
      <div>
        <button v-show="!isEdit" class="add" @click="beginAdd">+新增地址</button>
      </div>
		</div>
	</div>
</template>

<script>
        import AddressEdit from './AddressEdit.vue'
	export default {
                name: 'Address',
                components: {AddressEdit},
                data(){
                        return {
                                isEdit: false,
                        }
                },
                methods: {
                       async  setDefault(id){
                               try{
                                       await this.$store.dispatch("address/setDefault",id);
                                       this.$miNotice('默认地址设置成功..')
                               }catch(e){/**/}
                        },
                       async  remove(id){
                                try{
                                        this.$miConfirm('真删');
                                        this.$store.dispatch("address/remove",id);
                                        this.$miNotice('地址删除成功..')
                                }catch(e){/**/}
                        },
                        beginAdd(){
                                this.$refs.edit.beginEdit({
                                        id: 0,
                                        receiveName: "",
                                        receivePhone: "",
                                        receiveRegion: "",
                                        receiveDetail: "",
                                        isDefault: 0,

                                });
                                this.isEdit = true;
                        },
                        beginUpdate(address){
                                this.$refs.edit.beginEdit(address);
                                this.isEdit = true;
                        },
                         async save(address){
                                 try {
                                         if (address.id === 0) {
                                                 await this.$store.dispatch("address/add", address);
                                                 this.$miNotice("新增成功..");
                                         } else {
                                                 await this.$store.dispatch("address/update", address);
                                                 this.$miNotice("修改成功..");
                                         }
                                         this.isEdit = false;
                                 } catch (e) {
                                         /**/
                                 }
                        }
                        },
                  created(){
                        try{
                                this.$store.dispatch("address/init")
                        }catch(e){/**/}
                }
        }

</script>

<style scoped>

	.header{
		height: 53px;
	}

	.header span img {
		width: 43px;
		height: 53px;
		vertical-align: -20px;
		line-height: 53px;
	}
	.header span:nth-child(2) {
		color: rgb(51, 51, 51);
		font-size: 18px;
		width: 285px;
		margin-left:135px ;
		line-height: 53px;
	}

	.list li{
		padding-top: 20px;
		padding-bottom: 20px;
		padding-left:20px;
		border-bottom: 1px solid #e7e7e7;
	}
	.list li h6,
	.list li .phone{
		display: inline-block;
    margin-bottom: 10px;
	}
	.list li .phone{
		margin-left: 230px;
	}
  .list li p:nth-child(3){
    font-size: 14px;
  }
	.iconfont{
		font-size: 22px;
    color: rgba(184, 183, 183, 0.95);
	}
  .defaults{
    border: 1px solid red;
    display: inline;
  }
	.editbtn{
		margin-top: -20px;
		margin-left: 355px;
	}
	.footer {
		flex-shrink: 0;
		display: flex;
		justify-content: center;
		align-items: center;
	}
  .footer>div{
    position: absolute;
    top:660px;
  }
	.footer>div .add {
		border-color: rgb(132, 95, 63);
		border-radius: 5px;
		border-width: 1px;
		height: 44px;
		width: 337px;
		color: rgb(132, 95, 63);
		font-size: 14px;

	}
	.default{
		color: red;
	}
</style>